import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'
import { showToast } from "vant";
const routes: Array<RouteRecordRaw> = [
  {
    path: '/',
    name: 'Host',
    component: () => import('../views/Host.vue'),
    children: [
      {
        path: 'home',
        alias: '',
        name: 'Home',
        component: () => import('../views/Home.vue'),
      },
      {
        path: 'search',
        name: 'Search',
        component: () => import('../views/Search.vue'),
      },
      {
        path: 'orders',
        name: 'Orders',
        component: () => import('../views/Orders.vue'),
        beforeEnter: (to, from, next) => {
          if (sessionStorage.getItem("user")) {
            next()
          } else {
            showToast('账号未登录')
            setTimeout(() => {
              next({
                path: '/login'
              })
            }, 2000)
          }
        }
      },
      {
        path: 'my',
        name: 'My',
        component: () => import('../views/My.vue'),
        beforeEnter: (to, from, next) => {
          if (sessionStorage.getItem("user")) {
            next()
          } else {
            showToast('账号未登录')
            setTimeout(() => {
              next({
                path: '/login'
              })
            }, 2000)
          }
        }
      },
    ]
  },
  {
    path: '/delicacy',
    name: 'Delicacy',
    component: () => import('../views/Delicacy.vue'),
  },
  {
    path: '/info',
    name: 'Info',
    component: () => import('../views/Info.vue'),
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('../views/Login.vue'),
  },
  {
    path: '/address',
    name: 'Address',
    component: () => import('../views/Address.vue'),
  },
  {
    path: '/newaddress',
    name: 'Newaddress',
    component: () => import('../views/Newaddress.vue'),
  },
  {
    path: '/detail',
    name: 'Detail',
    component: () => import('../views/Detail.vue'),
    beforeEnter: (to, from, next) => {
      if (sessionStorage.getItem("user")) {
        next()
      } else {
        showToast('账号未登录')
        setTimeout(() => {
          next({
            path: '/login'
          })
        }, 2000)
      }
    }
  },
  {
    path: '/orderdetail',
    name: 'Orderdetail',
    component: () => import('../views/Orderdetail.vue'),
  },
  {
    path: '/reg',
    name: 'Reg',
    component: () => import('../views/Reg.vue'),
  },
  {
    path: '/noshop',
    name: 'Noshop',
    component: () => import('../components/Noshop.vue'),
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router
